import BaseView from "../../base/BaseView";
import BaseApp,{ViewZOrder} from "../../base/BaseApp";
import {easing, Layers, Node, RichText, size, Sprite, SpriteFrame, Tween, tween, UIOpacity, UITransform, v2 } from "cc";
import { bundleTool } from "../../base/bundleTool";
export enum TipsType {
	TC = "TOPCENTV", 
    BC = "BOTCENTV"
}
export default class TipsView extends BaseView {
	private static  _tipsConfs= {"TOPCENTV":{"imgPath":"common:img_rwxt/spriteFrame","imgSize":size(360,40),"pos":v2(0,200)},
		"BOTCENTV":{"imgPath":"common:img_rwxt/spriteFrame","imgSize":size(360,40),"pos":v2(0,-200)} };

	private _tipsList:any =[];

	private _data:any = null;

	public get tipsList() : any {
		return this._tipsList;
	}

	constructor(name:string) {
		super();
		this._data = TipsView._tipsConfs[name];
		this.node = new Node(name);
	}

	createView(strContent:string,npos:number):Node{
		const cloTips = new Node('Sprite')

		cloTips.layer = Layers.Enum.UI_2D;
		cloTips.setPosition(this._data.pos.x,(npos)*this._data.imgSize.height+this._data.pos.y);
		cloTips.addComponent(UITransform);
		cloTips.parent = this.node;
		const strs = this._data.imgPath.split(":");
		const rt = new Node("richtest");

		rt.layer = Layers.Enum.UI_2D;
		rt.parent = cloTips;
		const rtUI =  rt.addComponent(UITransform);
		const rtcom= rt.addComponent(RichText);

		rtcom.string = strContent;
		bundleTool.instance().loadAsyncFromBundle(strs[0], strs[1],SpriteFrame).then((sf:SpriteFrame) => {
			const sp = cloTips.addComponent(Sprite);

			sp.spriteFrame = sf;
			sp.type = Sprite.Type.SLICED;
			sp.sizeMode = Sprite.SizeMode.CUSTOM;
			if (rtUI.contentSize.width-10 > this._data.imgSize.width) {
				cloTips.getComponent(UITransform).setContentSize(size(rtUI.contentSize.width+10, this._data.imgSize.height))
			}else{
				cloTips.getComponent(UITransform).setContentSize(this._data.imgSize)
			}
		},(reason:string) => {
			console.error(reason);
		})

		this._tipsList[npos] = {"tip":cloTips,"txt":strContent}

		return cloTips;
	}
    
	public static createTips(strContent:string,cfgname:TipsType=TipsType.TC):void {
		let dlg = BaseApp.instance().getViewByName(cfgname) as TipsView;

		if(dlg == null){
			dlg= BaseApp.instance().openView(TipsView,cfgname,ViewZOrder.HighLayer)
			dlg.node.name = cfgname
		}

		let npos:number = dlg.tipsList.length;
		let nkey:number = 0;
		let cloTips:Node = null;

		for (const entry of dlg.tipsList) {
			if(entry!= null){
				if(entry.txt == strContent) {
					cloTips = entry.tip
					Tween.stopAllByTarget(cloTips);
					npos = nkey;
					break;
				} 
			}else{
				npos = nkey;
			}

			nkey = nkey+1;
		}

		if(cloTips==null) {
			cloTips = dlg.createView(strContent,npos);
		}

		const uio = cloTips.addComponent(UIOpacity);

		uio.opacity = 255;
		tween(uio).delay(2.0).to(1.8,{opacity:0},{ easing: easing.fade }).call(function(){
			if(npos>=dlg.tipsList.length-1) {
				dlg.tipsList.pop();
			}else {
				dlg.tipsList[npos] = null;
			}

			cloTips.destroy();
		}).start();  
	}
}